package cz.muni.fi.pa165.travelagency.cli;

import cz.muni.fi.pa165.travelagency.web.wsapi.PTourService;
import cz.muni.fi.pa165.travelagency.web.wsapi.PTourService_Service;
import cz.muni.fi.pa165.ws.travelagency.datatypes.PackageTourDTO;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jan Novotny
 */
public class CommandPackageTourService {
    
    private static PTourService ptourPort;
    
    public static void getPackageTour(String sid) {
        if (sid == null) {
            CommandParser.showError("Error: id has to be specified when retrieving package tour", true);
        }

        System.out.println("Getting package tour with id " + sid);

        try {
            Long id = Long.valueOf(sid);  
            PackageTourDTO pt = getPTourPort().getPackageTour(id);
            showPackageTour(pt);
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't retrieve package tour with given id", false);
        }
    }
    
    static void getAllPackageTours() {
        System.out.println("Getting all package tours");
        try {
            List<PackageTourDTO> result = getPTourPort().findAllPackageTours();
            if (result == null || result.isEmpty()) {
                System.out.println("No package tour found.");
            } else {
                System.out.println("All package tours:");
                for (PackageTourDTO p : result) {
                    showPackageTour(p);
                }
            }
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't retrieve all package tours", true);
        }
    }
    
    static void createPackageTour(String name, int capacity, String destination, String description, BigDecimal price, Calendar start, GregorianCalendar end) {
        PackageTourDTO pt = new PackageTourDTO();
        pt.setName(name);
        pt.setCapacity(capacity);
        pt.setDestination(destination);
        pt.setDescription(description);
        pt.setPricePerPerson(price);
        pt.setStart(start);
        pt.setEnd(end);

        System.out.println("Creating package tour:");
        showPackageTour(pt);
        try {
            getPTourPort().createPackageTour(pt);
            System.out.println("Package tour created");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't create new package tour.", true);
        }
    }

    static void updatePackageTour(Long id, String name, int capacity, String destination, String description, BigDecimal price, Calendar start, Calendar end) {
        PackageTourDTO pt = new PackageTourDTO();
        pt.setId(id);
        pt.setName(name);
        pt.setCapacity(capacity);
        pt.setDestination(destination);
        pt.setDescription(description);
        pt.setPricePerPerson(price);
        pt.setStart(start);
        pt.setEnd(end);

        System.out.println("Updating package tour:");
        showPackageTour(pt);
        try {
            getPTourPort().updatePackageTour(pt);
            System.out.println("Package tour updated");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't update package tour.", true);
        }
    }
    
    static void deletePackageTour(String id) {
        if (id == null) {
            CommandParser.showError("Error: id has to be specified when deleting package tour", true);
        }

        System.out.println("Removing package tour with id " + id);

        try {
            PackageTourDTO pt = getPTourPort().getPackageTour(Long.valueOf(id));
            showPackageTour(pt);
            getPTourPort().deletePackageTour(pt);
            System.out.println("Package tour removed");
        } catch (Exception e) {
            CommandParser.showError("Error: couldn't remove package tour with given id", true);
        }
    }

    private static PTourService getPTourPort() {
        if (ptourPort == null) {
            PTourService_Service service;
            try {
                service = new PTourService_Service(new URL(CommandParser.CONTEXT_PATH + "/PTourService?wsdl"));
                ptourPort = service.getPTourServicePort();
            } catch (MalformedURLException ex) {
                //should not happen
                Logger.getLogger(CommandPackageTourService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (ptourPort == null) {
            throw new RuntimeException("Cannot connect to the PTourService.");
        }
        return ptourPort;
    }
        
    private static void showPackageTour(PackageTourDTO p) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        System.out.println("PackageTour: id = " + p.getId() + ", name = " + p.getName() +
                ", capacity = " + p.getCapacity() + ", description = " + p.getDescription() + 
                ", destination = " + p.getDestination() + ", start = " + sdf.format(p.getStart().getTime()) + 
                ", end = " + sdf.format(p.getEnd().getTime()) + ", pricePerPerson = " + p.getPricePerPerson());
    }
}
